//
//  WeatherView.m
//  TheProjectName
//
//  Created by 李劲成 on 16/5/25.
//  Copyright © 2016年 sunzhichao. All rights reserved.
//

#import "WeatherView.h"
#import "WeatherData.h"
@interface WeatherView ()

@property (nonatomic, strong) UIImageView *temp_Photo;

@property (nonatomic, strong) UILabel *temp;

@property (nonatomic, strong) UILabel *weather;

@property (nonatomic, strong) WeatherData *model;

@end

@implementation WeatherView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self createSubViews];
        [self handleLocalData];
        [self handleData];
        [self configTimer];
    }
    return self;
}
- (void)createSubViews{

    self.temp_Photo = [[UIImageView alloc]initWithFrame:CGRectMake(10, FRAME_HEIGHT(self) * 0.33, FRAME_WIDTH(self) * 0.33, FRAME_HEIGHT(self) * 0.5)];
    [self addSubview:_temp_Photo];
    self.temp = [[UILabel alloc]initWithFrame:CGRectMake(20 + FRAME_WIDTH(self) * 0.33,FRAME_HEIGHT(self) * 0.6,FRAME_WIDTH(self) * 0.33, FRAME_HEIGHT(self) * 0.15)];
    [self addSubview:_temp];
    _temp.font = [UIFont systemFontOfSize:14];
    _temp.textColor = [UIColor whiteColor];
    self.weather = [[UILabel alloc]initWithFrame:CGRectMake(20 + FRAME_WIDTH(self) * 0.33,FRAME_HEIGHT(self) * 0.4, FRAME_WIDTH(self) * 0.33, FRAME_HEIGHT(self) * 0.2)];
    _weather.font = [UIFont systemFontOfSize:15];
    _weather.textColor = [UIColor whiteColor];
    [self addSubview:_weather];
}

- (void)handleLocalData{
    [[LocalCache sharedLocalCache] GETWithURL:[NSString stringWithFormat:@"http://apis.baidu.com/apistore/weatherservice/cityname?cityname=%@",@"大连"] Local:^(id localObj) {
        WeatherData *local = [[WeatherData alloc]init];
        [local setValuesForKeysWithDictionary:localObj[@"retData"]];
        _weather.text = local.weather;
        _temp.text = [NSString stringWithFormat:@"%@°~%@°", local.l_tmp, local.h_tmp];
        _temp_Photo.image = [UIImage imageNamed:local.weather];
    }];
}



- (void)configTimer{
    
    // 当前时间
    NSDate *currtentDate = [NSDate date];
    // 日期格式
    NSDateFormatter *dayFormatter = [[NSDateFormatter alloc]init];
    [dayFormatter setDateFormat:@"YYYY-MM-dd"];
    // 时间格式
    NSDateFormatter *timeFormatter = [[NSDateFormatter alloc]init];
    [timeFormatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
    
    // 字符串(当前日期)
    NSString *currtentDay = [dayFormatter stringFromDate:currtentDate];
    
    // (8点)
    NSDate *eightAM = [timeFormatter dateFromString:[NSString stringWithFormat:@"%@ 08:00:00", currtentDay]];
    
    if ([eightAM earlierDate:currtentDate] == eightAM) {
        eightAM = [[NSDate alloc]initWithTimeInterval:86400 sinceDate:eightAM];
        
    }
    NSTimer *eightTimer = [[NSTimer alloc]initWithFireDate:eightAM interval:86400 target:self selector:@selector(update) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop]addTimer:eightTimer forMode:NSDefaultRunLoopMode];
    
    // (11点)
    NSDate *elevenAM = [timeFormatter dateFromString:[NSString stringWithFormat:@"%@ 11:00:00", currtentDay]];
    if ([elevenAM earlierDate:currtentDate] == elevenAM) {
        elevenAM = [[NSDate alloc]initWithTimeInterval:86400 sinceDate:elevenAM];
    }
    NSTimer *elevenTimer = [[NSTimer alloc]initWithFireDate:elevenAM interval:86400 target:self selector:@selector(update) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop]addTimer:elevenTimer forMode:NSDefaultRunLoopMode];
    
    
    // (18点)
    NSDate *sixPM = [timeFormatter dateFromString:[NSString stringWithFormat:@"%@ 18:00:00", currtentDay]];
    if ([sixPM earlierDate:currtentDate] == sixPM) {
        sixPM = [[NSDate alloc]initWithTimeInterval:86400 sinceDate:sixPM];
    }
    NSTimer *sixTimer = [[NSTimer alloc]initWithFireDate:sixPM interval:86400 target:self selector:@selector(update) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop]addTimer:sixTimer forMode:NSDefaultRunLoopMode];
}

- (void)refreshWithData:(WeatherData *)data{
    _weather.text = data.weather;
    _temp.text = [NSString stringWithFormat:@"%@°~%@°", data.l_tmp, data.h_tmp];
    _temp_Photo.image = [UIImage imageNamed:data.weather];
}

- (void)handleData {
    [NetHandle GETWithURL:[NSString stringWithFormat:@"http://apis.baidu.com/apistore/weatherservice/cityname?cityname=%@",@"大连"] par:nil header:@{@"apikey":@"78c19dbf4a9ac55ce61c3100fbf7a14d"}  success:^(id responseObject, BOOL getUrl) {
        WeatherData *data = [[WeatherData alloc]init];
        [data setValuesForKeysWithDictionary:responseObject[@"retData"]];
        dispatch_async(dispatch_get_main_queue(), ^{
            [self refreshWithData:data];
        });
    } filed:^(NSError *error) {
        NSLog(@"%@", error);
    }];
}

- (void)update{
    [self handleData];
}


@end
